package com.mjfeed.controller.admin;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.PaymentMethod;
import com.mjfeed.entity.ShippingMethod;
import com.mjfeed.service.PaymentMethodService;
import com.mjfeed.service.ShippingMethodService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 支付方式
 * @author zhaominglei
 * @date 2014年10月9日
 */
@Controller("adminPaymentMethodController")
@RequestMapping("/admin/paymentMethod")
public class PaymentMethodController extends BaseController {
	@Resource(name="paymentMethodService")
	private PaymentMethodService paymentMethodService;
	@Resource(name="shippingMethodService")
	private ShippingMethodService shippingMethodService;
	
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/paymentMethod/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<PaymentMethod> pagination = paymentMethodService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/paymentMethod/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("shippingMethods", shippingMethodService.getAll(ShippingMethod.class));
		return "admin/paymentMethod/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(PaymentMethod paymentMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (paymentMethod == null) {
			return ERROR_VIEW;
		}
		Set<ShippingMethod> shippingMethods = new HashSet<ShippingMethod>();
		String[] shippingMethodIds = request.getParameterValues("shippingMethodId");
		for (String shippingMethodId : shippingMethodIds) {
			ShippingMethod shippingMethod = shippingMethodService.getById(ShippingMethod.class, Long.valueOf(shippingMethodId));
			shippingMethods.add(shippingMethod);
		}
		paymentMethod.setShippingMethods(shippingMethods);
		paymentMethodService.save(paymentMethod);
		return "redirect:/admin/paymentMethod/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("paymentMethod", paymentMethodService.getById(PaymentMethod.class, id));
		request.setAttribute("shippingMethods", shippingMethodService.getAll(ShippingMethod.class));
		return "admin/paymentMethod/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(PaymentMethod paymentMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (paymentMethod == null || paymentMethod.getId() == null || paymentMethod.getId() < 1) {
			return ERROR_VIEW;
		}
		PaymentMethod pPaymentMethod = paymentMethodService.getById(PaymentMethod.class, paymentMethod.getId());
		pPaymentMethod.setName(paymentMethod.getName());
		pPaymentMethod.setMethod(paymentMethod.getMethod());
		pPaymentMethod.setTimeout(paymentMethod.getTimeout());
		pPaymentMethod.setShippingMethods(null);
		Set<ShippingMethod> shippingMethods = new HashSet<ShippingMethod>();
		String[] shippingMethodIds = request.getParameterValues("shippingMethodId");
		for (String shippingMethodId : shippingMethodIds) {
			ShippingMethod shippingMethod = shippingMethodService.getById(ShippingMethod.class, Long.valueOf(shippingMethodId));
			shippingMethods.add(shippingMethod);
		}
		pPaymentMethod.setShippingMethods(shippingMethods);
		pPaymentMethod.setDescription(paymentMethod.getDescription());
		pPaymentMethod.setOrders(paymentMethod.getOrders());
		pPaymentMethod.setContent(paymentMethod.getContent());
		paymentMethodService.update(pPaymentMethod);
		return "redirect:/admin/paymentMethod/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("paymentMethod", paymentMethodService.getById(PaymentMethod.class, id));
		request.setAttribute("shippingMethods", shippingMethodService.getAll(ShippingMethod.class));
		return "admin/paymentMethod/view";
	}
}
